package com.bianguo.android.baidumapdemo.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.bianguo.android.baidumapdemo.BaseActivity;
import com.bianguo.android.baidumapdemo.R;
import com.bianguo.android.baidumapdemo.adapter.BaseRecyclerAdapter;
import com.bianguo.android.baidumapdemo.adapter.BezierGoodsAdapter;
import com.bianguo.android.baidumapdemo.utils.MLog;
import com.bianguo.android.baidumapdemo.utils.ScreenUtil;
import com.bianguo.android.baidumapdemo.widget.NXHolderView;

import java.util.ArrayList;

import butterknife.BindView;

public class GoodsActivity extends BaseActivity implements BaseRecyclerAdapter.OnClickItemListener {

    @BindView(R.id.goods_recycler)
    RecyclerView goodsRecycler;
    @BindView(R.id.goods_car)
    ImageView goodsCarIv;

    private BezierGoodsAdapter goodsAdapter;
    private ArrayList<String> goodsList;
    private LinearLayoutManager layoutManager;
    private int page = 1;
    private boolean isLoading;

    @Override
    protected int layoutId() {
        return R.layout.activity_goods;
    }

    @Override
    protected void initView(@Nullable Bundle saveInstanceState) {
        layoutManager = new LinearLayoutManager(mContext);
        goodsRecycler.setLayoutManager(layoutManager);
        goodsRecycler.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, R.anim.layout_anim_item_up));
        goodsList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            goodsList.add("item ---> " + i);
        }
        goodsAdapter = new BezierGoodsAdapter(mContext, goodsList);
        goodsRecycler.setAdapter(goodsAdapter);
        goodsRecycler.scheduleLayoutAnimation();
        goodsAdapter.setOnClickItemListener(this);
        goodsRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0 && page < 2) {
                    int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                    int childCount = layoutManager.getChildCount();
                    int itemCount = layoutManager.getItemCount();
                    if (!isLoading && (childCount + firstVisibleItemPosition) >= itemCount) {
                        isLoading = true;
                        page++;
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ArrayList<String> strings = new ArrayList<>();
                                int size = goodsList.size();
                                for (int i = size; i < size + 30; i++) {
                                    strings.add("item ---> " + i);
                                }
                                goodsAdapter.addAll(strings, false);
//                                goodsRecycler.scheduleLayoutAnimation();
                            }
                        }, 1000);
                    }
                }
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClickItem(BaseRecyclerAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.item_bezier_goods_reduce:
                break;
            case R.id.item_bezier_goods_add:
                final NXHolderView holderView = new NXHolderView(mContext);
                final ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
                rootView.addView(holderView);
                int[] startPoint = new int[2];
                view.getLocationInWindow(startPoint);
                int[] endPoint = new int[2];
                goodsCarIv.getLocationInWindow(endPoint);
//                这一段是自己画抛物线（贝塞尔曼曲线）
                int[] bracePoint = new int[2];
                bracePoint[0] = (startPoint[0] + endPoint[0]) / 2;
                bracePoint[1] = startPoint[1] - ScreenUtil.instance(mContext).dip2px(100);
                Path mPath = new Path();
                mPath.moveTo(startPoint[0], startPoint[1]);
                mPath.quadTo(bracePoint[0], bracePoint[1], endPoint[0], endPoint[1]);
                ObjectAnimator animator = ObjectAnimator.ofFloat(holderView, View.X, View.Y, mPath);
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        rootView.removeView(holderView);
                    }
                });
                animator.setDuration(400);
                animator.start();

//                holderView.setStartPosition(new Point(startPoint[0], startPoint[1]));
//                holderView.setEndPosition(new Point(endPoint[0], endPoint[1]));
//                holderView.startBezierAnimation();
                break;
        }
    }
}
